package engine.logic.loading.effect
{
	import flash.display.Shape;
	import flash.events.Event;
	
	import newx.StageInfo;

	/**
	 *圆圈特效 
	 * @author test
	 * 
	 */	
	public class Avulsion extends AbstractTransitionEffect
	{
		private static const EffectInOver:int = 0;
		private static const EffectOutOver:int = 1;
		//遮黑，恢复
		private var _type:int;
		//遮罩
		private var _avulsion:Shape;
		//当前stage的宽高
		private var _w:int;
		private var _h:int;
		//累加
		private var _plus:int;
		//速度
		private var _speed:int = 50;
		
		public function Avulsion()
		{
			super();
			_avulsion = new Shape();
			addChild(_avulsion);
		}
		
		override public function effectIn():void
		{
			_type = EffectInOver;
			_w = StageInfo.stage.stageWidth;
			_h = StageInfo.stage.stageHeight;
			_plus = 0;
			_avulsion.graphics.clear();
			_avulsion.graphics.beginFill(0x000000);
			
			addEventListener(Event.ENTER_FRAME, drawAvulsion);
		}
		
		override public function effectOut():void
		{
			_type = EffectOutOver;
			_w = StageInfo.stage.stageWidth;
			_h = StageInfo.stage.stageHeight;
			_plus = _w/_speed;
			_avulsion.graphics.clear();
			_avulsion.graphics.beginFill(0x000000);
			_avulsion.graphics.drawRect(0,0,_w,_h);
			
			addEventListener(Event.ENTER_FRAME, drawAvulsion);
		}
		
		override public function set width(value:Number):void
		{
			_w = value;
			if(_type == EffectOutOver)
			{
				_plus = _w - _plus*_speed;
			}
		}
		
		override public function set height(value:Number):void
		{
			_h = value;
		}
		
		private function drawAvulsion(e:Event):void
		{
			_avulsion.graphics.clear();
			_avulsion.graphics.beginFill(0x000000);
			_avulsion.graphics.drawCircle(_w/2, _h/2, _plus*_speed);
			
			
			if(_type == EffectInOver) 
			{
				_plus++;
				if(_plus*_speed > _w)
				{
					removeEventListener(Event.ENTER_FRAME, drawAvulsion);
					dispatchEvent(new TransitionEffectEvent(TransitionEffectEvent.EffectInOver));
				}					
			}
			else if(_type == EffectOutOver)
			{
				_plus--;
				if(_plus < 0)
				{
					_avulsion.graphics.clear();
					removeEventListener(Event.ENTER_FRAME, drawAvulsion);
					dispatchEvent(new TransitionEffectEvent(TransitionEffectEvent.EffectOutOver));
				}
			}
			
		}		
	}
}